package com.company.action;

import org.apache.struts2.ServletActionContext;

import com.company.entity.Accounts;
import com.company.service.AccountsService;
import com.sun.org.apache.regexp.internal.recompile;

public class AccountsAction extends BaseAction {

	private Accounts accounts;
	private AccountsService accountsService;

	public Accounts getAccounts() {
		return accounts;
	}

	public void setAccounts(Accounts accounts) {
		this.accounts = accounts;
	}

	public AccountsService getAccountsService() {
		return accountsService;
	}

	public void setAccountsService(AccountsService accountsService) {
		this.accountsService = accountsService;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ServletActionContext.getRequest().getSession()
				.setAttribute("msg", accountsService.query());
		return super.execute();
	}

	public String login() {
		if (accounts == null || accounts.getUsername() == null
				|| accounts.getPassword() == null) {
			return ERROR;
		}
		if (accountsService.login(accounts.getUsername(),
				accounts.getPassword())) {
			ServletActionContext.getRequest().getSession()
					.setAttribute("username", accounts.getUsername());
			ServletActionContext.getRequest().getSession()
					.setAttribute("password", accounts.getPassword());
			return "success";
		}
		return "error";
	}

	public String list() {
		
		ServletActionContext.getRequest().getSession()
				.setAttribute("username", accountsService.list().getUsername());
		ServletActionContext.getRequest().getSession()
				.setAttribute("password", accountsService.list().getPassword());
		return SUCCESS;
}
	
	public String update() throws Exception {
		if (accounts == null || accounts.getPassword() == null||accounts.getPassword().trim()=="") {
			return ERROR;
		}
		accountsService.update(accounts);
		return list();
	}

	public String nouser() throws Exception {
		return super.execute();
	}
}
